/**
 * Created by songj on 2015/12/11.
 */
define(['angular',
    'framework/msg/GillionMsgModule',
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/datasource/DataSourceModule',
    'framework/dropdown/GillionDropdownModule',
    'framework/date/DateModule',
    'angular-underscore'
], function (angular) {
    var module = angular.module('TestResultController',
        ['GillionMsgModule', 'DataGridModule', 'GillionPaginationModule',
            'DataSourceModule', 'DateModule', 'GillionDropdownModule'])
        .controller('TestResultController', ['$scope', 'GillionMsgService', '$dataSourceManager', '$timeout', 'Arrays', "$location",'Resource','GillionMsg','$compile','$http','$compile',
                function ($scope, GillionMsgService, $dataSourceManager, $timeout, Arrays, $location,Resource,GillionMsg,$compile) {
                    $scope.response = GillionMsgService.getInputData();
                    $scope.results = $scope.response.result.result;
                    $scope.logShow=true;
                    $scope.logOrbit=false;
                    var result="";
                    var log = "";
                    $scope.showLog = function(){
                        $scope.logDetail= log;
                        if($scope.logShow){
                            $scope.logShow=false;
                            $("#logDetail").css("height","0px");
                            $("#img").attr("class","icon-engine-upward-b");
                        }else{
                            $scope.logShow=true;
                            $("#logDetail").css("height","200px");
                            $("#img").attr("class","icon-engine-down-b");
                        }
                    };

                    $scope.showOrbit = function(){
                        $scope.logDetail= result;
                        if($scope.logOrbit){
                            $scope.logOrbit=false;
                            $scope.logShow=false;
                            $("#logDetail").css("height","0px");
                            $("#orbit").attr("class","icon-engine-upward-b");
                        }else{
                            $scope.logOrbit=true;
                            $scope.logShow=true;
                            $("#logDetail").css("height","200px");
                            $("#orbit").attr("class","icon-engine-down-b");
                        }
                    };



                    $timeout(function () {
                        var logDetail = $scope.response.result.success ? $scope.response.result.data : $scope.response.result.errorMessages[0],
                            lines = logDetail.split("\n"),
                            lineCount = lines.length,
                            $logDetail = angular.element("#logDetail");
                        $scope.logDetail = "";
                        log=logDetail;
                        var str =logDetail;
                        while (str.indexOf("\n开始执行规则：")>=0||str.indexOf("\n设置变量 ")>=0){
                            if(((str.indexOf("\n开始执行规则：")<str.indexOf("\n设置变量 "))||str.indexOf("\n设置变量 ")==-1)&&str.indexOf("\n开始执行规则：")!=-1) {
                                str =str.substr(str.indexOf("\n开始执行规则：")+1,str.length);
                                var ruleName = str.substr(0,str.indexOf("\n"));
                                result = result+ruleName+"\n\n";
                            } else {
                                str =str.substr(str.indexOf("\n设置变量 ")+1,str.length);
                                var varible = str.substr(0,str.indexOf("\n"));
                                result = result+varible+"\n\n";
                            }
                        }
                        if(logDetail.indexOf("开始匹配断言表达式")>=0){
                            var asset =logDetail.substr(logDetail.indexOf("\n开始匹配断言表达式：")+1,logDetail.length);
                            result = result+asset.substr(0,asset.indexOf("\n"))+"\n\n";
                        }
                        if(logDetail.indexOf("\n测试用例：")>=0){
                            var asset =logDetail.substr(logDetail.indexOf("\n测试用例：")+1,logDetail.length);
                            result = result+asset.substr(0,asset.indexOf("\n"))+"\n\n";
                        }

                        $scope.logDetail=logDetail;
                    },200);

                }
            ]
        );

    module.config(['$locationProvider', function($locationProvider) {
        $locationProvider.html5Mode(true);
    }]);
    module.filter("resultFilter",function(){
        return function(input){
            if(input==true){
                return "成功"
            }
            if(input==false){
                return "失败"
            }
        }
    })
    return module;
});
